<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="85px"
    >
      <el-form-item label="时间">
        <el-date-picker
          v-model="queryParams.daterange"
          type="daterange"
          align="right"
          size="small"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :clearable="false"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['report:checkPoint:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkPointList">
      <el-table-column type="index" label="序号" width="60" align="left" />

      <el-table-column
        label="部门"
        align="left"
        prop="deptName"
        minWidth="200"
      />
      <el-table-column
        label="日期"
        align="left"
        prop="effectDate"
        minWidth="100"
      />
      <el-table-column
        label="在线数"
        align="left"
        prop="onlineCount"
        minWidth="150"
      />
      <el-table-column
        label="离线数"
        align="left"
        prop="offlineCount"
        minWidth="150"
      />
      <el-table-column
        label="总数"
        align="left"
        prop="totalCount"
        minWidth="150"
      />
      <el-table-column
        label="上线率"
        align="left"
        prop="onlinePercent"
        minWidth="150"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { sumOnlineCount } from "@/api/report/personOnline";

import dayjs from "dayjs";

export default {
  name: "CheckPoint",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员,车辆检查点工作及里程统计表格数据
      checkPointList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,

        effectDate: undefined,
        daterange: [
          dayjs().subtract(7, "day").format("YYYY-MM-DD HH:mm:ss"),
          dayjs().format("YYYY-MM-DD HH:mm:ss"),
        ],
      },
    };
  },
  computed: {
    deptId() {
      return this.$store.getters.choosedDeptId;
    },
  },
  watch: {
    deptId: function () {
      this.queryParams.deptId = this.deptId;
      this.getList();
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询人员考勤统计 */
    getList() {
      this.loading = true;
      this.queryParams.deptId = this.deptId;

      if (
        this.queryParams.daterange &&
        this.queryParams.daterange.length == 2
      ) {
        this.queryParams.effectStart = dayjs(
          this.queryParams.daterange[0]
        ).format("YYYY-MM-DD");
        this.queryParams.effectEnd = dayjs(
          this.queryParams.daterange[1]
        ).format("YYYY-MM-DD");
      } else {
        this.queryParams.effectStart = null;
        this.queryParams.effectEnd = null;
      }
      sumOnlineCount(this.queryParams).then((response) => {
        this.checkPointList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.daterange = [
        dayjs().subtract(7, "day").format("YYYY-MM-DD HH:mm:ss"),
        dayjs().format("YYYY-MM-DD HH:mm:ss"),
      ];
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "report/personOnline/export",
        {
          ...this.queryParams,
        },
        `人员上线统计_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
